package com.linktco.common.launch;


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.launch.StartEventListener;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.net.InetAddress;

/**
 * @author zhangnx
 */
@Configuration(proxyBeanMethods = false)
@Slf4j
public class StartListener extends StartEventListener {


    @SneakyThrows
    @Override
    public void afterStart(WebServerInitializedEvent event) {
        Environment environment = event.getApplicationContext().getEnvironment();
        String appName = environment.getProperty("spring.application.name").toUpperCase();
        int localPort = event.getWebServer().getPort();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String profile = StringUtils.arrayToCommaDelimitedString(environment.getActiveProfiles());
        String format = String.format("---[%s]---启动完成,当前使用的端口:[%s],环境变量:[%s]---,接口地址：http://%s:%s",appName,localPort,profile,ip,localPort);
        System.out.println(format);
    }
}
